#Author:Math2Gold ( http://groups.google.com/group/math2gold ) 
#Last update:2009 Sep 11

import default_m2g_conf 
import m2g_misc
import datetime
import time
import xml.dom.minidom
from xml.dom.minidom import getDOMImplementation
from google.appengine.ext import db
from google.appengine.api import users
import logging

def getText(nodelist):
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc

class MBlogInfo(db.Model):
  name = db.StringProperty(required=True)
  screen_name = db.StringProperty(required=True)
  mblog_text = db.StringProperty(required=True,multiline=True)
  mblog_id = db.StringProperty(required=True)
  mblog_date = db.StringProperty(required=True)
  mblog_location = db.StringProperty()
  mblog_source = db.StringProperty()
  ftime=db.FloatProperty(required=True,indexed=True)

print 'Content-Type:text/plain'
print ''

document=m2g_misc.sendAuthHTTPContent(
                                      default_m2g_conf.twitter_username,
                                      default_m2g_conf.twitter_password,
                                      default_m2g_conf.twitter_timeline_xml,
                                      None)
dom = xml.dom.minidom.parseString(document)

m=dom.getElementsByTagName("status");
fftime=time.time();
ncount=0;
mresult="";
mmailresult="";

for x in m:
        mid=getText(x.getElementsByTagName("id")[0].childNodes)
	keyn="x"+mid
	exq =MBlogInfo.get_by_key_name(keyn)
	if    not ( exq == None) :
		continue
        mdate=getText(x.getElementsByTagName("created_at")[0].childNodes)
        mtext=getText(x.getElementsByTagName("text")[0].childNodes)
        msource=getText(x.getElementsByTagName("source")[0].childNodes)
        mname=getText(x.getElementsByTagName("user")[0].getElementsByTagName("name")[0].childNodes)
        msname=getText(x.getElementsByTagName("user")[0].getElementsByTagName("screen_name")[0].childNodes)
        mloc=getText(x.getElementsByTagName("user")[0].getElementsByTagName("location")[0].childNodes)
	e=MBlogInfo(name=mname,screen_name=msname,mblog_text=mtext,mblog_id=mid,mblog_date=mdate,mblog_location=mloc,mblog_source=msource,ftime=fftime,key_name=keyn)
	e.put()
	ncount=ncount+1
	mresult='= @'+msname+' '+mtext+' \n'+mresult
	mmailresult='---\n'+msname+'('+mdate+'):'+mtext+'\n'+mmailresult
    
###################################################################################

from google.appengine.api import xmpp
from google.appengine.api import mail

global user_gtalk,user_mail,send_mail
if ncount > 0 :
	user_address = default_m2g_conf.user_gtalk
	chat_message_sent = False
	if xmpp.get_presence(user_address):
		msg = mresult
		status_code = xmpp.send_message(user_address, msg)
		chat_message_sent = (status_code == xmpp.NO_ERROR)
	if not chat_message_sent:
            try:                
                logging.error('Send XMPP Error'+msg)
            except:    
                pass

	try:
		mail.send_mail(sender=default_m2g_conf.user_mail,to=default_m2g_conf.send_mail,subject="Twitter Info",body=mmailresult)
	except:
            try:
                logging.error('Send Mail Error'+mmailresult)
            except:
                pass


